/**
 * 声音方法
 */
/**
 * 播放音乐
 */
var playMusic = function (filename) {
    if (!filename) {
        filename = 'vbg' + settingData.bgmId;
    }
    if (!cc.sys.isNative) {
        return;
    }
    if (settingData.jingyinopen) {
        return;
    }
    if (!settingData.musicvoice) {
        return;
    }
    if (window.curBg == filename && cc.audioEngine.isMusicPlaying()) {
        return;
    }
    window.curBg = filename;
    stopMusic();
    var t = filename;
    var soundName = res[t];
    if (!soundName) {
        return;
    }
    var volume = settingData.yinyuePrecent;
    if (cc.sys.os == cc.sys.OS_ANDROID) {
        volume = volume / 500;
    } else {
        volume = volume / 1400;
    }
    if (cc.sys.isNative) {
        window.musicID = jsb.AudioEngine.play2d(soundName, true, volume);
    } else {
        window.musicID = cc.audioEngine.playMusic(soundName, true);
    }

    window.bgMusicState = "play";
};
/**
 * 暂停音乐
 */
var pauseMusic = function () {
    window.bgMusicState = "pause";
    if (cc.sys.isNative) {
        if (!_.isUndefined(window.musicID)) {
            jsb.AudioEngine.pause(window.musicID);
        }
    } else {
        cc.audioEngine.pauseMusic();
    }
};

/**
 * 恢复音乐
 */
var resumeMusic = function () {
    window.bgMusicState = "play";
    if (cc.sys.isNative) {
        if (!_.isUndefined(window.musicID)) {
            jsb.AudioEngine.resume(window.musicID);
        }
    } else {
        cc.audioEngine.resumeMusic();
    }
};
/**
 * 停止音乐
 */
var stopMusic = function () {
    window.bgMusicState = "stop";
    if (cc.sys.isNative) {
        if (!_.isUndefined(window.musicID)) {
            jsb.AudioEngine.stop(window.musicID);
            window.musicID = undefined;
        }
    } else {
        cc.audioEngine.stopMusic();
        window.musicID = undefined;
    }
};
var stopMusic_wailian = function () {
    if (!cc.sys.isNative) {
        return;
    }
    if (!_.isUndefined(window.musicID)) {
        // console.log("stop===:" + window.musicID);
        jsb.AudioEngine.pause(window.musicID);
    }
};

var resumeMusic_wailian = function () {
    if (!cc.sys.isNative)
        return;

    if (!_.isUndefined(window.musicID)) {
        // console.log('resume===:' + window.musicID);
        jsb.AudioEngine.resume(window.musicID);
    }
};
/**
 * 静音
 */
var muteVolume = function () {
    if (cc.sys.isNative) {
        jsb.AudioEngine.stopAll();
    } else {
        cc.audioEngine.stopMusic();
        cc.audioEngine.stopAllEffects();
    }
};
/**
 * 播音效
 * @param filename
 * @param sex
 */
var playEffect = function (filename, sex) {
    if (settingData.jingyinopen) {
        return;
    }
    if (!settingData.yinxiaovoice) {
        return;
    }

    var t = filename + '_' + (typeof sex === 'undefined' ? gameData.sex : sex);

    var soundRes = res;
    if (gameData.mapId && settingData.local_dialect && settingData.local_dialect[gameData.mapId] && window['dialect_res']) {
        soundRes = dialect_res;

        // fydp 地方话
        var local_dialect = settingData.local_dialect[gameData.mapId];
        if (cc.isString(local_dialect) && res.hasOwnProperty(local_dialect)) {
            soundRes = res[local_dialect];
        }
    }
    var soundName = soundRes[t] || soundRes[filename] || res[t] || res[filename];
    if (_.isArray(soundName)) {
        soundName = soundName[Math.floor(Math.random() * soundName.length)];
    }
    if (filename.indexOf('/res/') >= 0) {
        soundName = filename;
    }
    if (!soundName) {
        cc.log('sound not found: ' + t);
        return;
    }
    var volume = settingData.yinxiaoPrecent;
    if (cc.sys.os === cc.sys.OS_ANDROID) {
        volume = volume / 100;
    } else {
        volume = volume / 500;
    }
    if (cc.sys.isNative) {
        jsb.AudioEngine.play2d(soundName, false, volume);
    } else {
        cc.audioEngine.playEffect(soundName, false);
    }
};

/**
 * 播语音
 * @param filename
 */
var playNorEffect = function (filename) {
    if (!cc.sys.isNative) {
        return;
    }
    var volume = 1;
    if (cc.sys.os == cc.sys.OS_ANDROID) {
        volume = 1.5;
    }
    jsb.AudioEngine.play2d(filename, false, volume);
};
